package org.mall.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mall.common.constant.CommonConstant;
import org.mall.common.util.ObjectUtils;
import org.mall.common.util.RedisUtil;
import org.mall.modules.pojo.sys.params.SysSMSParams;
import org.mall.modules.pojo.sys.params.SystemParamsDTO;
import org.mall.modules.system.controller.SystemParamsConvertUtil;
import org.mall.modules.system.entity.SysParams;
import org.mall.modules.system.mapper.SysParamsMapper;
import org.mall.modules.system.service.ISysParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/***
 *@author yinxucun
 *@date 2019-11-01 10:50 
 */
@Service
public class SysParamsServiceImpl  extends ServiceImpl<SysParamsMapper, SysParams> implements ISysParamsService {
    @Resource
    private SysParamsMapper sysParamsMapper;

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public void saveOrUpdateBatchByName(List<SysParams> params) {
        QueryWrapper<SysParams> queryWrapper=new QueryWrapper();
        SysParams sqlData;
        for (SysParams temp:params){
            queryWrapper.eq("name",temp.getName());
            sqlData=getOne(queryWrapper);
            if (sqlData==null){
                save(temp);
            }
            else {
                temp.setId(sqlData.getId());
                updateById(temp);
            }
        }
    }

    @Override
    public SysSMSParams getSysSMSParams() {
        SysSMSParams sysSMSParams;
        Object o=redisUtil.get("mall:sms:params");
        if (o==null){
            QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", CommonConstant.MESSAGE_PARAMS_TYPE);
            List<SysParams> list = this.list(queryWrapper);
            List<SystemParamsDTO> list1= ObjectUtils.copyList(list,SystemParamsDTO.class);
            sysSMSParams=SystemParamsConvertUtil.getSysSMSPrams(list1);
            redisUtil.set("mall:sms:params",sysSMSParams);
        }
       else {
           sysSMSParams=(SysSMSParams)o;
        }
        return sysSMSParams;
    }

    @Override
    public boolean updateBatchByName(List<SysParams> params) {
        for (SysParams sysParams:params) {
            sysParamsMapper.updateByName(sysParams);
        }
        return true;
    }
}
